<?php
namespace app\index\controller;

use think\Controller;
use think\View;

class Api extends controller
{   
	public function index(){
        header('Content-Type: text/plain; charset=utf-8');
       
        //$access_token = $this->getAccessToken();//获取用户openid与session_key
        $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=7_2GA0M41OppcWXUnefUBBSzLxp7h4xg7IxeLi-Q3c_cW_a9SMqVtfJ2n_1t5qFAWusNiLYHxO48jVMKA45TimM0WSL6KxqxhcT29KOHTJ7KTcy7nUBr4LF17zriO55wJzjfkEveDhhrRxpGTXWWYcAGAEEG";
        $data['path']  = "/pages/cueInfo/cueInfo?query=1";
        $data['width'] = "200";
        
        $result = httpRequest($url,json_encode($data));
        dump($result);
        file_put_contents("qrcode/$id.png", $result);
        return "qrcode/$id.png";
    }

    public function back(){
         //请求微信退还用户的押金
        $out_trade_no  = '201804222201441LVa5h2g44H05704';
        $nonce_str     = getRandCode(16);//生成随机的32位字符串
        $out_refund_no = "4567890djioedmcmdcwecwwec";//商户的订单号

        $total_fee = 9390;
        $deposit   = 9300;

        $prepay = array(
                    'appid'            => 'wxa641018b058e4ac9', //小程序qppid
                    'mch_id'           => '1326035601', 
                    'nonce_str'        => $nonce_str,//生成随机的32位字符串
                    'op_user_id'       => '1326035601',
                    'out_refund_no'    => $out_refund_no,
                    'out_trade_no'     => $out_trade_no,//商户的订单号
                    'refund_fee'       => $deposit,
                    'total_fee'        => $total_fee       
                  );
        
        $xml = "<xml>
                     <appid>wxa641018b058e4ac9</appid>
                     <mch_id>1326035601</mch_id>
                     <nonce_str>".$nonce_str."</nonce_str>
                     <op_user_id>1326035601</op_user_id>
                     <out_refund_no>".$out_refund_no."</out_refund_no>
                     <out_trade_no>".$out_trade_no."</out_trade_no>
                     <refund_fee>".$deposit."</refund_fee>
                     <total_fee>".$total_fee."</total_fee>
                     <sign>".$this->MakeSign($prepay)."</sign>
                 </xml>";
        //调用api，自定义对参数进行处理，改请求方式是自定义方式 
        $xml_result = curlRequest($xml);

        $result     = json_decode(json_encode(simplexml_load_string($xml_result, 'SimpleXMLElement', LIBXML_NOCDATA)),true);
        dump($result);
        return $result['return_code'] == 'SUCCESS' ? true : false;
       
    }
    
     public function MakeSign($prepay)
    {
        //签名步骤一：按字典序排序参数
        ksort($prepay);
        $string = $this->ToUrlParams($prepay);
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=d57f65ebb69106cd68d1aacd7b8616d7";
        //签名步骤三：MD5加密
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }

    /**
     * 格式化参数格式化成url参数
     */
    public function ToUrlParams($prepay)
    {
        $buff = "";
        foreach ($prepay as $k => $v)
        {
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= $k . "=" . $v . "&";
            }
        }

        $buff = trim($buff, "&");
        return $buff;
    }
}
